package cn.edu.zufe.mjt;

import java.util.concurrent.locks.LockSupport;

/**
 * @author 蒙锦涛
 * @date 2025/5/29
 * @description 测试LockSupport的使用
 */
public class LockSupportTest {


    private void printA(Thread thread) {
        try {
            Thread.sleep(2L);
            System.out.println("A");
            LockSupport.unpark(thread);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void printB(Thread thread) {
        try {
            Thread.sleep(1L);
            LockSupport.park();
            System.out.println("B");
            LockSupport.unpark(thread);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private void printC() {
        try {
            Thread.sleep(3L);
            LockSupport.park();
            System.out.println("C");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        LockSupportTest l = new LockSupportTest();
        Thread tC = new Thread(l::printC);
        Thread tB = new Thread(()->l.printB(tC));
        Thread tA = new Thread(()->l.printA(tB));
        tA.start();
        tB.start();
        tC.start();
    }

}
